package com.shazhu.api.nongjt;

import com.shazhu.common.core.controller.BaseController;
import com.shazhu.common.core.domain.AjaxResult;
import com.shazhu.nongjt.domain.NjtPrinterDevice;
import com.shazhu.nongjt.service.INjtPrinterDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 序列号管理Controller
 *
 * @author shazhu
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/api/serial")
public class NjtSerialController extends BaseController
{
    @Autowired
    private INjtPrinterDeviceService njtPrinterDeviceService;

    /**
     * 检查序列号并连接设备
     */
    @GetMapping("/checkSerialCode")
    public AjaxResult checkSerialCode(String serialCode)
    {
        try {
            NjtPrinterDevice printer = njtPrinterDeviceService.selectBySerialCode(serialCode);
            if (printer == null) {
                return AjaxResult.error("未找到该打印机");
            }

            if (printer.getAuthStatus() != 1) {
                return AjaxResult.error("该打印机未完成认证，请前往个人中心完成打印机序列号认证，或联系技术人员提供帮助，谢谢.");
            }

            // 更新最后连接时间
            njtPrinterDeviceService.updateLastConnectTime(printer.getId());

            return AjaxResult.success("设备连接成功!");
        } catch (Exception e) {
            logger.error("检查序列号失败", e);
            return AjaxResult.error("设备连接失败");
        }
    }

    /**
     * 连接MAC地址
     */
    @PostMapping("/linkMac")
    public AjaxResult linkMac(@RequestBody NjtPrinterDevice njtPrinterDevice)
    {
        try {
            int result = njtPrinterDeviceService.linkPrinterByMac(njtPrinterDevice);
            return AjaxResult.success("连接成功", result);
        } catch (Exception e) {
            logger.error("连接MAC失败", e);
            return AjaxResult.error("连接失败");
        }
    }
}